library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
---------------------------------------------------------------------------------------
entity vga is
generic(	h_ta:	integer:=96;
			h_tb:	integer:=40;
			h_tc:	integer:=8;
			h_td:	integer:=640;
			h_te:	integer:=8;
			h_tf:	integer:=8;
			h_blank:integer:=144;
			h_total:integer:=800;
			len:	integer:=128;
			wid:	integer:=128;
			l_ta:	integer:=2;
			l_tb:	integer:=25;
			l_tc:	integer:=8;
			l_td:	integer:=480;
			l_te:	integer:=8;
			l_tf:	integer:=2;
			l_brank:integer:=35;
			l_total:integer:=525);

port(		clk50m:	in	std_logic;
			rst:	in	std_logic;
			q:		in std_logic_vector(23 downto 0);
			vga_clock:	out std_logic;
			vga_hs:	buffer	std_logic;
			vga_ls:	buffer	std_logic;
			vga_blank:	out std_logic;
			vga_sync:	out std_logic;
			vga_r:	out std_logic_vector(7 downto 0);
			vga_g:	out std_logic_vector(7 downto 0);
			vga_b:	out std_logic_vector(7 downto 0);
			vga_r0:	out std_logic_vector(1 downto 0);
			vga_g0:	out std_logic_vector(1 downto 0);
			vga_b0:	out std_logic_vector(1 downto 0);
			rom_clock:out std_logic;
			address1:out	std_logic_vector(13 downto 0)
			);
end vga;
----------------------------------------------------------------------------------------
architecture behav of vga is
	signal clk25m:	std_logic;
	signal h_count:	integer	range 0 to 2000;
	signal l_count:	integer	range 0 to 2000;
	signal address:	std_logic_vector(13 downto 0);
	signal xpos:	integer	range 0 to 1000;
	signal ypos:	integer	range 0 to 1000;
	signal ij:		std_logic_vector(1 downto 0);

begin

vga_r0<="00";
vga_g0<="00";
vga_b0<="00";
vga_clock<=clk25m;
rom_clock<=clk25m;
address1<=address;

	fenpin:process(clk50m)
			begin
				if(clk50m'event and clk50m='1')then
					clk25m<=not clk25m;
				end if;
			end process;
	
	brank: process(h_count,l_count)
			begin
				if((h_count<h_blank)or(l_count<l_brank))then
					 vga_blank<='0';
				else vga_blank<='1';
				end if;
			end process;
	
	h_tongbu:process(clk25m)
			begin
				if(clk25m'event and clk25m='1')then
					if(rst='0')then
						h_count<=0;
						vga_hs<='1';
					elsif(h_count<h_total)then
							h_count<=h_count+1;
						else h_count<=0;
					end if;
					if(h_count<=h_ta-1)then
						vga_hs<='0';
					else vga_hs<='1';
					end if;
				end if;
			end process;
			
	l_tongbu:process(vga_hs)
			begin
				if(vga_hs'event and vga_hs='1')then
					if(rst='0')then
						l_count<=0;
						vga_ls<='1';
					elsif(l_count<l_total)then
						l_count<=l_count+1;
						else l_count<=0;
					end if;
					
					if(l_count<=l_ta-1)then
						vga_ls<='0';
						else vga_ls<='1';
					end if;
				end if;
			end process;
			
	shuju:	process(clk25m)
			begin
				if(clk25m'event and clk25m='1')then
					if(rst='0')then address<="00000000000000";
						elsif(h_count<(h_ta+h_tb+h_tc+len) and (h_count>=h_ta+h_tb+h_tc)
							and (l_count<l_ta+l_tb+l_tc+wid)
							and (l_count>=l_ta+l_tb+l_tc))
							then address<=address+'1';
							vga_r<=q(23 downto 16);
							vga_g<=q(15 downto 8);
							vga_b<=q(7 downto 0);
						else
							vga_r<="00000000";
							vga_g<="00000000";
							vga_b<="00000000";
					end if;
				end if;
			end process;
			


end behav;
					
			



